package com.woupla.search;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.SelectEvent;

import com.woupla.city.CityEJB;
import com.woupla.company.CompanyEJB;
import com.woupla.model.City;
import com.woupla.model.Planing;

@Named
@ViewScoped
public class SearchController implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3240109470249165386L;
	@Inject
	private CompanyEJB companyEJB;
	@Inject
	private SearchEJB searchEJB;
	@Inject
	private CityEJB cityEJB;
	
	private String citySelected;
	
	private List<Planing> planings;
	private Planing planingSelected;
	
	public List<City> searchCity(final String query){
		String temp = query.replace(" (", ":");
		temp = temp.replace(")", ":");
		String[] values = temp.split(":");		
		String city = values[0];
		String cp = "";
		if(values.length>1)
			cp = values[1];
	    return cityEJB.findCityLikeCpOrLabel(city);
	}
	
	public void handleModifFiltre(final SelectEvent event) {
		String temp = citySelected.replace(" (", ":");
		temp = temp.replace(")", ":");
		String[] values = temp.split(":");		
		String city = values[0];
		String cp = "";
		setPlanings(searchEJB.findPlaning(city));
	}

	/**
	 * @return the citySelected
	 */
	public String getCitySelected() {
		return citySelected;
	}

	/**
	 * @param citySelected the citySelected to set
	 */
	public void setCitySelected(String citySelected) {
		this.citySelected = citySelected;
	}

	/**
	 * @return the planings
	 */
	public List<Planing> getPlanings() {
		return planings;
	}

	/**
	 * @param planings the planings to set
	 */
	public void setPlanings(List<Planing> planings) {
		this.planings = planings;
	}

	/**
	 * @return the planingSelected
	 */
	public Planing getPlaningSelected() {
		return planingSelected;
	}

	/**
	 * @param planingSelected the planingSelected to set
	 */
	public void setPlaningSelected(Planing planingSelected) {
		this.planingSelected = planingSelected;
	}
	
	
}
